package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniuxy.commons.exception.StoreException;
import com.woniuxy.commons.exception.code.StoreExceptionCode;
import com.woniuxy.commons.model.Result;
import com.woniuxy.commons.utils.BeanCopyUtil;
import com.woniuxy.controller.form.GetAllStoreForm;
import com.woniuxy.controller.form.UpdateMyStoreForm;
import com.woniuxy.dao.StoreInfoDao;
import com.woniuxy.dao.model.StoreInfo;
import com.woniuxy.service.StoreInfoService;
import com.woniuxy.service.dto.MyStoreDto;
import com.woniuxy.service.dto.PageGetAllStoreDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 顾客信息 前端控制器
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@RestController
@RequestMapping("/storeInfo")
@Api(tags = "商家基本信息操作接口")
public class StoreInfoController {

    @Autowired
    private StoreInfoService storeInfoService;
    @Resource
    private StoreInfoDao storeInfoDao;

    /**
     * 超级管理员查询所有商家信息，分页
     *
     * @return
     */
    @ApiOperation("超级管理员查询所有商家信息，分页")
    @PostMapping("/getAllStore")
    public Result getAllStore(@RequestBody GetAllStoreForm gasf) {

        PageGetAllStoreDto allStore = storeInfoService.getAllStore(gasf);

        return Result.success(200, "查询所有商家信息成功", allStore);
    }

    /**
     * 超级管理员激活商家接口
     * @param storeId 商家id
     * @return
     */
    @ApiOperation("超级管理员激活商家接口")
    @GetMapping("/active/{storeId}")
    @ApiImplicitParam(name = "storeId", value = "商家id")
    public Result active(@PathVariable Integer storeId) {

        StoreInfo store = storeInfoService.getById(storeId);
        if (store.getStatus() == 1) {
            throw new StoreException(StoreExceptionCode.STORE_HAS_ACTIVE);
        }

        store.setStatus(1);

        storeInfoService.update(store, new QueryWrapper<StoreInfo>().eq("id", storeId));

        return Result.success(200, "激活成功", null);
    }

    /**
     * 超级管理员冻结商家接口
     * @param storeId 商家id
     * @return
     */
    @ApiOperation("超级管理员冻结商家接口")
    @GetMapping("/frozen/{storeId}")
    @ApiImplicitParam(name = "storeId", value = "商家id")
    public Result frozen(@PathVariable Integer storeId) {

        StoreInfo store = storeInfoService.getById(storeId);
        if (store.getStatus() == 0) {
            throw new StoreException(StoreExceptionCode.STORE_HAS_FROZEN);
        }

        store.setStatus(0);

        storeInfoService.update(store, new QueryWrapper<StoreInfo>().eq("id", storeId));

        return Result.success(200, "冻结成功", null);
    }

    /**
     * 商家查询自己店铺的信息
     *
     * @param
     * @return
     */
    @GetMapping("/getMyStore")
    @ApiOperation("商家查询自己店铺信息接口")
    public Result getMyStore() throws JsonProcessingException {

        MyStoreDto myStore = storeInfoService.getMyStore();

        return Result.success(200, "查询我的店铺信息成功", myStore);

    }

    /**
     * 商家查询自己店铺的信息
     *
     * @param id 商家id
     * @return
     */
    @GetMapping("/list/{id}")
    @ApiOperation("通过id查询商家信息")
    @ApiImplicitParam(name = "id", value = "商家id")
    public Result getStoreByid(@PathVariable Long id) {
        return Result.success(200, "店铺信息查询成功",
                storeInfoService.getById(id));

    }

    /**
     * 商家修改自己店铺的信息,包括店铺logo上传
     *
     * @return
     */
    @PostMapping("/updateMyStore")
    @ApiOperation("商家修改自己店铺的信息的接口,包括店铺logo上传")
    public Result updateMyStore(@RequestBody UpdateMyStoreForm udmf) {

        StoreInfo storeInfo = BeanCopyUtil.copyOne(udmf, StoreInfo.class);

        storeInfoService.update(storeInfo, new QueryWrapper<StoreInfo>().eq("id", udmf.getId()));

        return Result.success(200, "修改成功", null);
    }

    /**
     * 首页商家展示
     * @return
     */
    @GetMapping("list")
    @ApiOperation("首页商家展示")
    public Result showList() {
        return Result.success(200, "查询成功", storeInfoDao.storeShow());
    }

    /**
     * 获取所有商家信息
     * @return
     */
    @GetMapping("/listAll")
    @ApiOperation("获取所有商家信息")
    public Result getAllStores() {
        return Result.success(200, "查询成功", storeInfoService.list());
    }

}

